package com.lhb.rabbit.service;

import com.lhb.rabbit.config.MqConstants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class JsonPublisher {

    @Autowired
    private RabbitTemplate jsonRabbitTemplate;

    public String publish(String name){
        return publish1(name)+publish2(name);
    }

    private String publish1(String ans){
        Map<String, Object> map = new HashMap<>(8);
        map.put("msg",ans);
        map.put("type","json");
        map.put("version","123");
        System.out.println("publish: "+map);
        jsonRabbitTemplate.convertAndSend(MqConstants.exchange, MqConstants.routing,map);
        return map.toString();
    }

    private String publish2(String ans){
        BasicPublisher.NonSerDo nonSerDo = new BasicPublisher.NonSerDo(18,"self_json"+ans);
        System.out.println("publish: "+ nonSerDo);
        return nonSerDo.toString();
    }
}
